#ifndef IDATABASE_H
#define IDATABASE_H

#include <QtPlugin>
#include <QObject>

#include "../definitions.h"
#include "../core/core.h"

#ifdef Q_WS_WIN
#define MY_EXPORT __declspec(dllexport)
#else
#define MY_EXPORT
#endif


//a few useful things
typedef struct PLUGININFO{
	QString shortName;
	quint32 version;
	QString versionString;
	QString description;
	QString author;
	QString authorEmail;
	QString copyright;
	QString homepage;
} PLUGININFO;

inline quint32 PLUGIN_VERSION(quint32 a, quint32 b, quint32 c, quint32 d){
	return (((a & 0xFF) << 24) | ((b & 0xFF) << 16) |
			((c & 0xFF) << 8) | (d & 0xFF) );
}

inline QString VERSION_TO_STRING(quint32 version){
	quint8 build = (version & 0xFFFFFF);
	quint8 revision = (version & 0xFFFF) >> 8;
	quint8 minor = (version & 0xFFFFFF) >> 16;
	quint8 major = version >> 24;

	QString versionString = QString::number(major) + "." +
							QString::number(minor) + "." +
							QString::number(revision) + "." +
							QString::number(build);
	return versionString;
}

//method that return some info before load plugin by PluginLoader
extern "C" MY_EXPORT PLUGININFO getPluginInfo(void);


//a few useful things
typedef struct ChannelEntry{
	quint32 id;
	QString title;
	QString link;
	QString url;
	QString desc;
	QString favicon;
}ChannelEntry;

typedef struct NewsEntry{
	quint32 id;
	quint32 channel;
	QString title;
	QByteArray desc;
	quint32 org_size;
	QString link;
	quint32 pubdate;
	quint32 lastOperation;
	bool readed;
	bool deleted;
}NewsEntry;

//class/interface
class IDatabase : public QObject
{
    Q_OBJECT
protected:
	QString m_databaseName;
	QString m_hostName;
	QString m_password;//fixme should be saved not in plain text
	QString m_userName;
	int m_port;

	Core *m_pCore;
public:
	explicit IDatabase(QObject *parent = 0);
	virtual ~IDatabase();

	void init(Core *singleton);

	virtual bool connectToDB() = 0;
	virtual bool disconnectFromDb() = 0;
	virtual bool canConnect() const = 0;

	virtual void setSSL( bool ssl ) = 0;
	virtual bool useSSL() const = 0;

	virtual int getDefaultPort() const = 0;


	//change or check data in database
	virtual quint64 checkLogin(const QString &login,
							   const QString &password) = 0;

	virtual bool addLogin(const QString &login,
						  const QString &password) = 0;

	virtual bool changePassword(const quint64 &id,
								const QString &oldPassword,
								const QString &newPassword) = 0;

	virtual QList<ChannelEntry> getChannels(const quint64 &id) = 0;
	virtual QList<NewsEntry> getNews(const quint64 &id,
									 const quint64 &channel,
									 const quint32 &lastSync) = 0;
	virtual bool addChannel(const quint64 &id,
							const ChannelEntry &entry) = 0;
	virtual bool subscribeChannel(const quint64 &id,
								  const quint64 &channel) = 0;
	virtual bool updateNews(const quint64 &id,
							const quint32 &news,
							const quint32 &timestamp,
							bool readed, bool deleted) = 0;

	//connection property
	void setDatabaseName(const QString &dbName);
	QString getDatabaseName() const;

	void setHostName(const QString &host);
	QString getHostName() const;

	void setUserName(const QString &userName);
	QString getUserName() const;

	void setPassword(const QString &password);
	QString getPassword() const;

	void setPort(const int &port);
	int getPort() const;

	void setConnection( const QString &host, const QString &userName,
						const QString &password, const int &port = -1);

signals:
	void pluginInitiated();

};

QT_BEGIN_NAMESPACE
Q_DECLARE_INTERFACE(IDatabase,
					"pl.com.momus.beaver.IDatabase/0.0.7")
QT_END_NAMESPACE

#endif // IDATABASE_H
